<?php
/**
 * PayAction.class.php (支付模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class PayAction extends BaseAction
{

	function _initialize()
    {	
		parent::_initialize();
		if(!$this->_userid){
                    $this->assign('jumpUrl',U('User/Login/index'));
                    $this->error(L('nologin'));
		}
		$this->dao = M('User');
		$this->assign('bcid',0);
		$user = $this->dao->find($this->_userid);
		$this->assign('vo',$user);
    }

    public function index()
    {
        $this->display();
    }

    public function payanswer()
    {
        $this->display();
    }
    
    public function Recharge()
    {
        $Payment = M('Payment')->where("pay_code<>'Balance'")->order("id")->select();
        $this->assign('Payment',$Payment);
        $this->display();
    }
	
    public function orderpay()
    {
        $orderid=intval($_REQUEST['id']);
        $field="id,regulartime,product_name,amount,order_amount,number,add_time,incometime,ordertype";
        $vvo=M("Order")->field($field)->find($orderid);
        $this->assign('vvo',$vvo);
        $result=M("User")->field("amount")->where("id=".$this->_userid)->select();
        $useramount=$result[0]['amount'];
        $this->assign('useramount',$useramount);
        $this->display();
    }

    public function pay()
    {
        $orderid=intval($_REQUEST['id']);
        $order_amount=get_safe_replace($_REQUEST['order_amount']);
        $olduseramount=get_safe_replace($_REQUEST['useramount']);  
        //减用户余额
        $r =M('User')->where("id = '$this->_userid'")->setDec('amount',$order_amount);
        if($r){
                $orderup['id'] = $orderid;
                $orderup['status'] = 1;
                $orderup['pay_status'] = 1;
                $orderup['pay_time'] =time();
                M('Order')->save($orderup);
              $this->display("Pay:success");   
        }else{
              $this->error ( L('do_error'));
        }     
    }
	public function respond()
	{
		$pay_code = !empty($_REQUEST['code']) ? get_safe_replace($_REQUEST['code']) : '';	
		$pay_code = ucfirst($pay_code);
		$Payment = M('Payment')->getByPayCode($pay_code);
		if(empty($Payment))$this->error(L('PAY CODE EROOR!'));
		$aliapy_config = unserialize($Payment['pay_config']);		 
		import("@.Pay.".$pay_code);
		$pay=new $pay_code($aliapy_config);
		$r = $pay->respond();	
		$this->assign('jumpUrl',URL('User-Order/index'));
		if($r){
			$this->error(L('PAY_OK'));
		}else{
			$this->error(L('PAY_FAIL'));
		}
	}
 
}
?>